package com.funny.learning.english.adapter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;

import com.androidquery.AQuery;
import com.funny.learning.english.R;
import com.funny.learning.english.model.HistoryEntity;

import android.content.Context;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class HistoryAdapter extends BaseAdapter {
	private AQuery mAQuery;

	private LayoutInflater mLayoutInflater;

	private ArrayList<HistoryEntity> historyEntities;

	public HistoryAdapter(Context context,
			ArrayList<HistoryEntity> historyEntities) {
		this.historyEntities = historyEntities;
		mLayoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mAQuery = new AQuery(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return historyEntities.size();
	}

	@Override
	public HistoryEntity getItem(int position) {
		// TODO Auto-generated method stub
		return historyEntities.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.history_item, null);
			viewHolder = new ViewHolder(convertView);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		HistoryEntity historyEntity = getItem(position);

//		AQuery aQuery = mAQuery.recycle(convertView);
		if(historyEntity != null){
			viewHolder.tvNameGame.setText("Game " + historyEntity.getGame());
			viewHolder.tvCorrect.setText("Trả lời đúng: "
					+ historyEntity.getDung_count() + "/"
					+ historyEntity.getTong_count());
			viewHolder.tvDate.setText(getDate(historyEntity.getTime()));
			viewHolder.tvTime.setText(getHours(historyEntity.getTime()));
		}
		return convertView;
	}

	private static String getDate(long time) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(time);
		String date = DateFormat.format("dd-MM-yyyy", cal).toString();
		return date;
	}

	private static String getHours(long time) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(time);
		String date = DateFormat.format("hh:mm a", cal).toString();
		return date;
	}

	private static class ViewHolder {
		ImageView ivAvatar;
		TextView tvNameGame;
		TextView tvCorrect;
		TextView tvDate;
		TextView tvTime;

		public ViewHolder(View v) {
			ivAvatar = (ImageView) v.findViewById(R.id.ivAvatar);
			tvNameGame = (TextView) v.findViewById(R.id.tvNameGame);
			tvCorrect = (TextView) v.findViewById(R.id.tvCorrect);
			tvDate = (TextView) v.findViewById(R.id.tvDate);
			tvTime = (TextView) v.findViewById(R.id.tvTime);
			v.setTag(this);
		}
	}

}
